//
//  AnimationDelegate.m
//  BaseCode
//
//  Created by 曹连华 on 2019/7/4.
//  Copyright © 2019 曹连华. All rights reserved.
//

#import "AnimationDelegate.h"
@interface AnimationDelegate ()

@property (nonatomic, copy) AnimationDidStartBlock didStar;
@property (nonatomic, copy) AnimationDidStopBlock didStop;

@end

@implementation AnimationDelegate

+ (instancetype)animationDidStartBlock:(nullable AnimationDidStartBlock)didStar
                      animationDidStop:(nullable AnimationDidStopBlock)didStop {
    AnimationDelegate *delegate = [[AnimationDelegate alloc]initWithAnimationDidStartBlock:didStar animationDidStop:didStop];
    return delegate;
}

- (instancetype)initWithAnimationDidStartBlock:(nullable AnimationDidStartBlock)didStar
                              animationDidStop:(nullable AnimationDidStopBlock)didStop {
    self = [super init];
    if (self) {
        self.didStar = didStar;
        self.didStop = didStop;
    }
    return self;
}
#pragma mark - CAAnimationDelegate
- (void)animationDidStart:(CAAnimation *)anim {
    if (self.didStar) {
        self.didStar(anim);
    }
}

/**
 * flag参数表明了动画是自然结束还是被打断,
 比如调用了removeAnimationForKey:方法或removeAnimationForKey方法，flag为NO，如果是正常结束，flag为YES
 */
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (self.didStop) {
        self.didStop(anim,flag);
    }
    if (flag) {
        NSLog(@"动画 - 正常结束! anim = %@ flag = %d",anim,flag);
    } else {
        NSLog(@"动画 - 被打断结束! anim = %@ flag = %d",anim,flag);
    }
    
}

@end
